#include "s0.h"
#include "types.h"
#include "q_perm.h"

#include <assert.h>

byte s0(byte value, byte* key, int key_size)
{
	assert(key_size >= 2 && key_size <= 4);
	byte result = value;
	if(key_size == 4)
		result = q1_perm(result) ^ key[3];
	if(key_size >= 3)
		result = q1_perm(result) ^ key[2];
	result = q0_perm(result);
	result ^= key[1];
	result = q0_perm(result);
	result ^= key[0];
	result = q1_perm(result);
	return result;
}
